<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
      if (!empty($_SESSION['id'])) {
       $id=$_SESSION['id'];
        // var_dump($id);
        $admin=M('user')->find($id);
        // var_dump($admin);
        $this->assign('admin',$admin);

        $this->show();
      }else{
        $this->error('你没登陆，请登录');
      }
        
    }
   // 我的桌面
     public function welcome(){
         // 判断是否存储统计数据缓存
         if(S('welcomeData')){
        // var_dump(888);
          $data = S('welcomeData');
         }else{
        // var_dump(777);
         $data = $this->countData();
         S('welcomeData',$data ,3600);
         }
         
         // var_dump()
      	$this->assign('data',$data);
      	$this->show();
     }
   // 统计数据
     public function countdata(){
           $str= date('y-m-d');//当前日期

           $time = strtotime($str);//当前时间戳
 
           $yesterday=strtotime("-1 days",$time);//昨天时间戳

           $week=strtotime("-1 week",$time);//一星期前的时间戳

           $month=strtotime("-1 month",$time);//一月前的时间戳

           $data=array();
           
           // 统计总数
           $sql="select count(id) cn from user";
           $res=M()->query($sql);
           $data['总数']['user']=$res[0]['cn'];

           $sql="select count(id) cn from question";
           $res=M()->query($sql); 
           $data['总数']['question']=$res[0]['cn'];

           $sql="select count(id) cn from answer";
           $res=M()->query($sql); 
           $data['总数']['answer']=$res[0]['cn'];

           // 统计今天的总数
           $sql="select count(id) cn from user where create_time>'{$time}'";
           $res=M()->query($sql);
           $data['今天']['user']=$res[0]['cn'];

           $sql="select count(id) cn from question where create_time>'{$time}'";
           $res=M()->query($sql); 
           $data['今天']['question']=$res[0]['cn'];

           $sql="select count(id) cn from answer where create_time>'{$time}'";
           $res=M()->query($sql); 
           $data['今天']['answer']=$res[0]['cn'];

           // 统计昨天的数据
           $sql="select count(id) cn from user where create_time>'{$yesterday}' and create_time<'{$time}'";
           $res=M()->query($sql);
           $data['昨天']['user']=$res[0]['cn'];

           $sql="select count(id) cn from question where create_time>'{$yesterday}' and create_time<'{$time}'";
           $res=M()->query($sql); 
           $data['昨天']['question']=$res[0]['cn'];

           $sql="select count(id) cn from answer where create_time>'{$yesterday}' and create_time<'{$time}'";
           $res=M()->query($sql); 
           $data['昨天']['answer']=$res[0]['cn'];

           // 统计一星期内的数据
           $sql="select count(id) cn from user where create_time>'{$week}'";
           $res=M()->query($sql);
           $data['周']['user']=$res[0]['cn'];

           $sql="select count(id) cn from question where create_time>'{$week}'";
           $res=M()->query($sql); 
           $data['周']['question']=$res[0]['cn'];

           $sql="select count(id) cn from answer where create_time>'{$week}'";
           $res=M()->query($sql); 
           $data['周']['answer']=$res[0]['cn'];

            // 统计一月内的数据
           $sql="select count(id) cn from user where create_time>'{$month}'";
           $res=M()->query($sql);
           $data['月']['user']=$res[0]['cn'];

           $sql="select count(id) cn from question where create_time>'{$month}'";
           $res=M()->query($sql); 
           $data['月']['question']=$res[0]['cn'];

           $sql="select count(id) cn from answer where create_time>'{$month}'";
           $res=M()->query($sql); 
           $data['月']['answer']=$res[0]['cn'];

           return $data;
     }
  

 
}